﻿function userLogin() {
	$('#divName').css('display', 'none');
	$('#divPwd').css('display', 'none');
	$('#divCode').css('display', 'none');
	$('#divLoginMsg').css('display', 'none');

	showMessage('#divLoginMsg', '正在登录,请稍候...', true);

	var unCtrl = $('#txtUserName');
	var pwdCtrl = $('#txtPassword');
	var codeCtrl = $('#txtIdentityCode');
	var userName = unCtrl.val();
	var pwd = pwdCtrl.val();
	var code = codeCtrl.val();

	if (userName == '') {
		showMessage('#divName', '请输入用户名。');
		$('#divLoginMsg').css('display', 'none');
		return;
	}
	if (pwd == '') {
		showMessage('#divPwd', '请输入密码。');
		$('#divLoginMsg').css('display', 'none');
		return;
	}
	if (code == '') {
		showMessage('#divCode', '请输入验证码。');
		$('#divLoginMsg').css('display', 'none');
		return;
	}

	$.ajax({
		type : 'POST',
		url : "ajax/login.do",
		data : {
			password : pwd,
			username : userName,
			identitycode : code
		},
		success : function(request) {
			$('#divLoginMsg').css('display', 'none');
			if (request.msg == 'SUC') {
				$('#divLoginMsg').css('display', 'none');
				if (document.referrer) {
					document.location = document.referrer;
				} else {
					document.location = 'index.do';
				}
			} else {
				changeCode();
				$('#divLoginMsg').css('display', 'none');
				switch (request.msg) {
				case "-1":
					showMessage('#divName', '用户名或密码错误！');
					unCtrl.focus();
					break;
				case "-2":
					showMessage('#divCode', '验证码错误！');
					codeCtrl.focus();
					break;
				default:
					showMessage('#divName', '请输入用户名、密码和验证码！');
					break;
				}
				return;

			}
		},
		dataType : 'json'
	});
}

function showMessage(divName, message, messageType) {
	var divCtrl = $(divName);
	if (divCtrl != null) {
		if (messageType) {
			divCtrl.addClass('Send_Ok');
		} else {
			divCtrl.addClass('error');
		}
		divCtrl.css('display', 'block');
		divCtrl.html(message);
	}
}

function changeCode() {
	var img = $('#imgCode');
	var ran = Math.random() * 1000;
	img.attr('src', 'generateValidateCode.do?randomcode=' + ran);
}

function GetFlowDetail(type, typeid, st, et) {
	loading();
	$
			.getJSON(
					"/ajax/flowdetail.do",
					{
						un : typeid,
						et : et,
						st : st
					},
					function(data) {
						if (data && data.msg == "suc") {
							aa = "<h3>" + type + "</h3>";
							aa = aa
									+ "<table><thead><tr><th>时间</th><th>交易金额</th><th>交易号</th><th>备注</th></tr></thead><tbody>";
							for ( var i = 0; i < data.flows.length; i++) {
								aa = aa
										+ "<tr><td style='text-align:center;'>"
										+ data.flows[i].flowtime
										+ "</td><td>"
										+ data.flows[i].flowchange
										+ "</td><td style='text-align:center;'>"
										+ data.flows[i].flowpayid
										+ "</td><td style='text-align:left;'>"
										+ data.flows[i].flowmemo + "</td></tr>";
							}
							aa = aa + "</tbody></table>";
							unloading();
							$("#loading_img").html(aa);
							$("#loading_img").css('display', 'block');
						} else
							unloading();
					});
}

function UpdateRights(user) {
	var user = user;
	rights = document.forms[0].rights;

	txt = ""
	for (i = 0; i < rights.length; ++i) {
		if (rights[i].checked) {
			txt = txt + rights[i].value + "^"
		}
	}
	$.getJSON("/ajax/right.do", {
		rights : txt,
		userId : user
	}, function(data) {
		if (data && data.msg == "suc") {
			alert('设置成功！');
		} else {
			alert('设置失败！');
		}
	});
}

function OnSelect(obj) {
	var parent = obj.parent();
	parent.children("tr").each(function() {
		$(this).removeClass('highlight');
	});
	obj.addClass('highlight');

}

function loading() {
	$("#loading_img").css('display', 'block');
	$('#loading_img').html('<img src="/res/img/loading.gif"/>');
}
function unloading() {
	$("#loading_img").css('display', 'none');
	$('#loading_img').html();
}